package com.hrbxlh.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * -------------------------------------------------------
 * Copyright (c) 2017, 鑫联华信息科技股份有限公司
 * All rights reserved.
 * FileName：SessionValues.java
 * Description：获取session中村的数据的util类。
 * History：
 * Date           Author               Desc
 * 2017.3.29		  xzx                 建立
 * -------------------------------------------------------
 */

public class SessionValues {

	/***@description 获取session中的登录信息
	 * @param keysArray 需要获取的key值list 请求对象request
	 * @param getMsgKey session中的key值 attribute
	 * @return Map 通过需要的key获取相应的value
	 * @throws Exception
	 */
	public Map<String, Object> getSessionValues(String[] keysArray, HttpServletRequest request, String getMsgKey){

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try{
			HttpSession session = request.getSession();
			//获取session中存储的加密字节数组
			Object userMsg = session.getAttribute(getMsgKey);
			if(userMsg == null){
				return map;
			}
			//map = getSessionValueDecode(userMsg);
			//将存入的数据解密，并返回一个map
			if(getMsgKey.equals("userMsg")){
				map = (Map<String, Object>)session.getAttribute(getMsgKey);
				for(int i = 0; i < keysArray.length; i++){
					returnMap.put(keysArray[i], map.get(keysArray[i]));
				}
			}else{
				for(int i = 0; i < keysArray.length; i++){
					returnMap.put(keysArray[i], session.getAttribute(keysArray[i]));
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return map;
		}
		return returnMap;
	}

}
